package cn.iocoder.yudao.module.bpm.message.rabbitmq;

import cn.iocoder.yudao.module.bpm.message.ProcessInstanceMessage;
import cn.iocoder.yudao.module.bpm.message.dto.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

@Log4j2
public class RabbitMqProcessInstanceMessage implements ProcessInstanceMessage {

    private final RabbitTemplate rabbitTemplate;

    private final TopicExchange processInstanceCleanTopicExchange;
    private final TopicExchange processInstanceFinishedTopicExchange;
    private final TopicExchange processInstanceRejectTopicExchange;
    private final TopicExchange processInstanceCreatedTopicExchange;
    private final TopicExchange processInstanceStartedExchange;

    public RabbitMqProcessInstanceMessage(
            RabbitTemplate rabbitTemplate,
            TopicExchange processInstanceCleanTopicExchange,
            TopicExchange processInstanceFinishedTopicExchange,
            TopicExchange processInstanceRejectTopicExchange,
            TopicExchange processInstanceCreatedTopicExchange,
            TopicExchange processInstanceStartedExchange
    ) {
        this.rabbitTemplate = rabbitTemplate;
        this.processInstanceCleanTopicExchange = processInstanceCleanTopicExchange;
        this.processInstanceFinishedTopicExchange = processInstanceFinishedTopicExchange;
        this.processInstanceRejectTopicExchange = processInstanceRejectTopicExchange;
        this.processInstanceCreatedTopicExchange = processInstanceCreatedTopicExchange;
        this.processInstanceStartedExchange = processInstanceStartedExchange;
    }
    @Override
    public void onProcessInstanceCleanMsg(BpmProcessInstanceCancelledMessage bpmProcessInstanceCancelledMessage) {

        rabbitTemplate.convertAndSend(processInstanceCleanTopicExchange.getName(), null, bpmProcessInstanceCancelledMessage);
    }

    @Override
    public void onProcessInstanceFinishedMsg(BpmProcessInstanceApproveMessage bpmProcessInstanceApproveMessage) {
        rabbitTemplate.convertAndSend(processInstanceFinishedTopicExchange.getName(), null, bpmProcessInstanceApproveMessage);
    }

    @Override
    public void onProcessInstanceRejectMsg(BpmProcessInstanceRejectMessage bpmProcessInstanceRejectMessage) {
        rabbitTemplate.convertAndSend(processInstanceRejectTopicExchange.getName(), null, bpmProcessInstanceRejectMessage);
    }

    @Override
    public void onProcessInstanceCreatedMsg(BpmProcessInstanceCreatedMessage bpmProcessInstanceCreatedMessage) {
        rabbitTemplate.convertAndSend(processInstanceCreatedTopicExchange.getName(), null, bpmProcessInstanceCreatedMessage);
    }

    @Override
    public void onProcessInstanceStartedMsg(BpmProcessInstanceStartedMessage bpmProcessInstanceStartedMessage) {
        rabbitTemplate.convertAndSend(processInstanceStartedExchange.getName(), null, bpmProcessInstanceStartedMessage);
    }
}
